﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.Entities;
using BookStore.Solution.BLL;

namespace BookStore.Solution.Presentation
{
    public partial class FrmCategory : DevComponents.DotNetBar.OfficeForm
    {
        private string cateType = "Sách";
        private bool addNew = true;
        public bool viewDetail = false;
        Category category = null;
        CategoryManager categoryManager;

        public FrmCategory()
        {
            InitializeComponent();
        }
        public FrmCategory(bool type)
        {
            InitializeComponent();
            if (!type)
                cateType = "Văn phòng phẩm";

        }
        public FrmCategory(string cateID, bool type, bool detail)
        {
            InitializeComponent();
            if (!type)
                cateType = "Văn phòng phẩm";
            this.Text = "Cập nhật thông tin danh mục: " + cateID;
            txtCategoryID.Enabled = false;
            addNew = false;
            if (detail)
            {
                viewDetail = true;
                btnReset.Visible = false;
                btnSubmit.Text = "Đóng cửa sổ";
                btnSubmit.Width = 90;
                btnSubmit.Location = new Point(((int)this.Width / 2), btnSubmit.Location.Y);
                this.Text = "Xem thông tin danh mục: " + cateID;
            }
            categoryManager = new CategoryManager();
            category = categoryManager.GetDetail(cateID);
            txtCategoryID.Text = category.CategoryID.ToString();
            txtCategoryName.Text = category.CategoryName.ToString();
            txtDescription.Text = category.Description.ToString();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (viewDetail)
            {
                this.Close();
            }
            else
            {
                string result = "";
                if (checkValidation())
                {
                    category = new Category();
                    category.CategoryID = txtCategoryID.Text;
                    category.CategoryName = txtCategoryName.Text;
                    category.Description = txtDescription.Text;
                    category.Type = cateType;
                    category.Status = "Khả dụng";
                    categoryManager = new CategoryManager();
                    if (addNew)
                    {
                        result = categoryManager.Insert(category);
                    }
                    else
                    {
                        result = categoryManager.Update(category);

                    }
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Yêu cầu nhập đầy đủ thông tin", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtCategoryID.ResetText();
            txtCategoryName.ResetText();
            txtDescription.ResetText();
        }
        private bool checkValidation()
        {
            bool result = true;
            if (txtCategoryID.Text == "")
            {
                txtCategoryID.Border.BackColor = Color.FromArgb(255, 0, 0);
                result = false;
            }
            if (txtCategoryName.Text == "")
            {
                txtCategoryName.Border.BackColor = Color.FromArgb(255, 0, 0);
                result = false;
            }
            return result;
        }
    }
}